package cn.bgs.controller;


import cn.bgs.common.R;
import cn.bgs.config.DirectConfig;
import cn.bgs.pojo.FeedbackForm;
import cn.bgs.pojo.PersonnelInfo;
import cn.bgs.service.FeedbackFormService;
import cn.bgs.util.OrderUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 反馈表 前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/feedback-form")
@Slf4j
public class FeedbackFormController {

    @Autowired
    private FeedbackFormService feedbackFormService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/feedbackforminfo")
    @ApiOperation(value = "反馈信息查询")
    public R projectManageInfo(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String fbFormPerson){
        QueryWrapper<FeedbackForm> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("fb_form_person",fbFormPerson);
        queryWrapper.orderByDesc("create_time").eq("delete_flag",0);
        Page<FeedbackForm> page = feedbackFormService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return R.ok().data("villagepersonnelinfo" , page);
    }

    @RequestMapping("/updatedInfo")
    @ApiOperation(value = "反馈信息回复")
    public R updatedInfo(@RequestBody FeedbackForm feedbackForm){

        try {
//            String msg = param.get("msg").toString();

            //发送消息到交换机
            //参数： 交换机、路由键、消息体
            rabbitTemplate.convertAndSend(DirectConfig.DIRECT_EXCHANGE_NAME, DirectConfig.DIRECT_ROUTING_KEY_NAME, feedbackForm);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error();
        }
    }

    @RequestMapping("/addInfo")
    @ApiOperation(value = "反馈信息添加")
    public R addInfo(@RequestBody FeedbackForm feedbackForm){
        LocalDateTime localDateTime = LocalDateTime.now();
        feedbackForm.setOfferAdviceId(1);
        feedbackForm.setFeedbackFormNumber(OrderUtils.getDateTime());
        feedbackForm.setOfferTime(localDateTime);
        feedbackForm.setOfferState("正在进行");
        feedbackForm.setDeleteFlag(0);
        feedbackForm.setCreateTime(localDateTime);
        feedbackForm.setUpdateTime(localDateTime);
        boolean save = feedbackFormService.save(feedbackForm);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }

    @RequestMapping("/deleteInfo/{id}")
    @ApiOperation(value = "反馈信息删除")
    public R addInfo(@PathVariable Integer id){

        boolean save = feedbackFormService.removeById(id);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }

}

